package com.ctg.behavior.calc.impala.builder.retentions;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.ctg.behavior.common.consts.CtFieldConsts;

@Component
public class CtRetentionsRowKeyBuilder {

	public List<String> listByValues(List<String> byFields, Map<String, Object> oneCalcResult) {
		List<String> byValues = new ArrayList<>();
		for (String property : byFields) {
			byValues.add(String.valueOf(oneCalcResult.get(property)));
		}
		return byValues;
	}

	public String buildResultKey(String calcDate, String eventId, String eventDate, List<String> byValues) {
		StringBuilder sbuilder = new StringBuilder("RK@");
		sbuilder.append(calcDate);
		sbuilder.append("@").append(eventId);
		sbuilder.append("@").append(eventDate);
		for (String byValue : byValues) {
			sbuilder.append("@").append(byValue);
		}
		return sbuilder.toString();
	}

	public String buildResultKey(List<String> byFields, Map<String, Object> oneCalcResult) {
		StringBuilder sbuilder = new StringBuilder("RK@");
		sbuilder.append(oneCalcResult.get(CtFieldConsts.FIELD_KEY_CALC_DATE));
		sbuilder.append("@").append(oneCalcResult.get(CtFieldConsts.FIELD_KEY_EVENT));
		sbuilder.append("@").append(oneCalcResult.get(CtFieldConsts.FIELD_KEY_EVENT_DATE));
		for (String property : byFields) {
			sbuilder.append("@").append(oneCalcResult.get(property));
		}
		if (byFields.isEmpty()) {
			sbuilder.append("@").append(CtFieldConsts.FIELD_KEY_ALL);
		}
		return sbuilder.toString();
	}
	
	public String buildSumRowKey(List<String> byValues) {
		return "";
	}

	public String buildRowKey(List<String> byFields, Map<String, Object> oneCalcResult) {
		StringBuilder sbuilder = new StringBuilder("RK");
		if (byFields.isEmpty()) {
			sbuilder.append("@").append(CtFieldConsts.FIELD_KEY_ALL);
		}
		for (String property : byFields) {
			sbuilder.append("@").append(oneCalcResult.get(property));
		}
		return sbuilder.toString();
	}

	public String buildDayRowKey(String whichDay, List<String> byFields, Map<String, Object> oneCalcResult) {
		StringBuilder sbuilder = new StringBuilder("RK");
		if (byFields.isEmpty()) {
			sbuilder.append("@").append(CtFieldConsts.FIELD_KEY_ALL);
		}
		for (String property : byFields) {
			sbuilder.append("@").append(oneCalcResult.get(property));
		}
		sbuilder.append("@").append(whichDay);
		return sbuilder.toString();
	}

}
